package com.bot.adms.dao.impl;

import com.bot.adms.dao.IAdSpScDetailTextDao;

import com.bot.adms.model.entity.AdSpScDetailText;
import com.bot.adms.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 文字广告信息持久层实现类
 *
 * @author 石景坤
 */
public class AdSpScDetailTextDaoImpl implements IAdSpScDetailTextDao {
    /**
     * 插入海报信息
     *
     * @param adSpScDetailText 文字广告信息，包含（标识，图片地址，显示位置，字体类型，字体大小，字体颜色，文字内容广告分屏明细id）
     * @return 成功条数
     */
    @Override
    public Integer insertAdSpScDetailText(AdSpScDetailText adSpScDetailText) {
        Connection conn = null;
        Statement statement = null;
        //1.连接数据库
        try {
            //1.连接数据库
            conn = JdbcUtils.getConnection();

            //2.获取数据库执行器
            statement = conn.createStatement();

            //3.定义sql语句
            String sql = "INSERT INTO `ad_sp_sc_detail_text` (`image_url`,`location`,`font_type`,`font_size`,`font_color`,`text_content`,`ad_sp_sc_detail_id`) VALUES (" +
                    "'" + adSpScDetailText.getImageUrl() + "'," +
                    "" + adSpScDetailText.getLocation() + "," +
                    "'" + adSpScDetailText.getFontType() + "'," +
                    "" + adSpScDetailText.getFontSize() + "," +
                    "'" + adSpScDetailText.getFontColor() + "'," +
                    "'" + adSpScDetailText.getTextContent() + "'," +
                    "'" + adSpScDetailText.getAdSpScDetailId() + "')";
            //4.执行sql
            int count = statement.executeUpdate(sql);

            //5.返回成功条数
            return count;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭数据库
            JdbcUtils.close(conn, statement);
        }
    }

    /**
     * 更新海报信息
     *
     * @param adSpScDetailText 文字广告信息，包含（标识，图片地址，显示位置，字体类型，字体大小，字体颜色，文字内容广告分屏明细id）
     * @return 成功条数
     */
    @Override
    public Integer updateAdSpScDetailText(AdSpScDetailText adSpScDetailText) {
        Connection conn = null;
        Statement statement = null;
        //1.连接数据库
        try {
            //1.连接数据库
            conn = JdbcUtils.getConnection();

            //2.获取数据库执行器
            statement = conn.createStatement();

            //3.定义sql语句
            String sql = "update `ad_sp_sc_detail_text` set";

            //3.2判断条件并拼接
            if (adSpScDetailText.getImageUrl() != null && adSpScDetailText.getImageUrl().trim().length() > 0) {
                sql += " `image_url`='" + adSpScDetailText.getImageUrl() + "',";
            }
            if (adSpScDetailText.getLocation() != null) {
                sql += " `location`='" + adSpScDetailText.getLocation() + "',";
            }
            if (adSpScDetailText.getFontType() != null) {
                sql += " `font_type`='" + adSpScDetailText.getFontType() + "',";
            }
            if (adSpScDetailText.getFontSize() != null) {
                sql += " `font_size`='" + adSpScDetailText.getFontSize() + "',";
            }
            if (adSpScDetailText.getFontColor() != null) {
                sql += " `font_color`='" + adSpScDetailText.getFontColor() + "',";
            }
            if (adSpScDetailText.getTextContent() != null) {
                sql += " `text_content`='" + adSpScDetailText.getTextContent() + "'";
            }
            sql += " where id=" + adSpScDetailText.getId();

            //4.执行sql
            int count = statement.executeUpdate(sql);

            //5.返回成功条数
            return count;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭数据库
            JdbcUtils.close(conn, statement);
        }
    }

    /**
     * 删除海报信息
     *
     * @param id 海报标识
     * @return 成功条数
     */
    @Override
    public Integer deleteAdSpScDetailText(Long id) {
        Connection conn = null;
        Statement statement = null;
        //1.连接数据库
        try {
            //1.连接数据库
            conn = JdbcUtils.getConnection();

            //2.获取数据库执行器
            statement = conn.createStatement();

            //3.定义sql语句
            String sql = "delete from `ad_sp_sc_detail_text` where id=" + id;

            //4.执行sql
            int count = statement.executeUpdate(sql);

            //5.返回成功条数
            return count;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭数据库
            JdbcUtils.close(conn, statement);
        }
    }

    /**
     * 查找海报信息
     *
     * @param id 海报标识
     * @return 文字广告信息，包含（标识，图片地址，显示位置，字体类型，字体大小，字体颜色，文字内容广告分屏明细id）
     */
    @Override
    public AdSpScDetailText getAdSpScDetailTextById(Long id) {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        //1.连接数据库
        try {
            //1.连接数据库
            conn = JdbcUtils.getConnection();

            //2.获取数据库执行器
            statement = conn.createStatement();

            //3.定义sql语句
            String sql = "select `id`,`image_url`,`location`,`font_type`,`font_size`,`font_color`,`text_content`,`ad_sp_sc_detail_id`  from `ad_sp_sc_detail_text` where id=" + id;

            //4.执行sql获取结果集
            rs = statement.executeQuery(sql);

            //只有一条数据通过if判断，将指针跳到数据行
            if (rs.next()) {
                AdSpScDetailText adSpScDetailText = new AdSpScDetailText();
                adSpScDetailText.setId(rs.getLong("id"));
                adSpScDetailText.setImageUrl(rs.getString("image_url"));
                adSpScDetailText.setLocation(rs.getInt("location"));
                adSpScDetailText.setFontType(rs.getInt("font_type"));
                adSpScDetailText.setFontSize(rs.getInt("font_size"));
                adSpScDetailText.setFontColor(rs.getString("font_color"));
                adSpScDetailText.setTextContent(rs.getString("text_content"));
                adSpScDetailText.setAdSpScDetailId(rs.getLong("ad_sp_sc_detail_id"));

                //5.返回搜索到的对象
                return adSpScDetailText;
            }

            return null;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭数据库
            JdbcUtils.close(conn, statement, rs);
        }
    }

    /**
     * 查询海报信息
     *
     * @param adSpScDetailText 查询条件 包含（标识，图片地址，显示位置，字体类型，字体大小，字体颜色，文字内容广告分屏明细id）
     * @return 查询到的信息列表包含（标识，图片地址，显示位置，字体类型，字体大小，字体颜色，文字内容广告分屏明细id）
     */
    @Override
    public List<AdSpScDetailText> searchAdSpScDetailText(AdSpScDetailText adSpScDetailText) {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        List<AdSpScDetailText> list = new ArrayList<AdSpScDetailText>();
        //1.连接数据库
        try {
            //1.连接数据库
            conn = JdbcUtils.getConnection();

            //2.获取数据库执行器
            statement = conn.createStatement();

            //3.定义sql语句
            String sql = "select `id`,`image_url`,`location`,`font_type`,`font_size`,`font_color`,`text_content`,`ad_sp_sc_detail_id`  from `ad_sp_sc_detail_text` where 1=1";

            //3.2根据条件拼接
            if (adSpScDetailText.getId() != null) {
                sql += " and id=" + adSpScDetailText.getId();
            }
            if (adSpScDetailText.getAdSpScDetailId() != null) {
                sql += " and ad_sp_sc_detail_id=" + adSpScDetailText.getAdSpScDetailId();
            }

            //4.执行sql获取结果集
            rs = statement.executeQuery(sql);

            while (rs.next()) {
                //将结果集数据存储到adSpScDetailPoster中
                AdSpScDetailText adSpScDetailText1 = new AdSpScDetailText();
                adSpScDetailText1.setId(rs.getLong("id"));
                adSpScDetailText1.setImageUrl(rs.getString("image_url"));
                adSpScDetailText1.setLocation(rs.getInt("location"));
                adSpScDetailText1.setFontType(rs.getInt("font_type"));
                adSpScDetailText1.setFontSize(rs.getInt("font_size"));
                adSpScDetailText1.setFontColor(rs.getString("font_color"));
                adSpScDetailText1.setTextContent(rs.getString("text_content"));
                adSpScDetailText1.setAdSpScDetailId(rs.getLong("ad_sp_sc_detail_id"));

                //将对象插入到数组中
                list.add(adSpScDetailText1);
            }


            return list;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭数据库
            JdbcUtils.close(conn, statement, rs);
        }
    }
}
